<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :inline="true" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="姓名" prop="name" style="width: 45%">
      <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex" style="width: 45%">
      <template>
          <el-radio v-model="dataForm.sex" label="男" border>男</el-radio>
          <el-radio v-model="dataForm.sex" label="女" border>女</el-radio>
      </template>
    </el-form-item>
    <el-form-item label="生日" prop="birthday" style="width: 45%">
        <el-date-picker
          v-model="dataForm.birthday"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="选择日期"
          style="width: 86%">
        </el-date-picker>
    </el-form-item>
    <el-form-item label="地址"  style="width: 45%">
      <el-cascader
        clearable
        placeholder="请选择期望城市"
        :props="{ value: 'id',label:'address' }"
        style="width: 100%"
        v-model="dataForm.address"
        :options="addressOptions"
        :show-all-levels="false"
      ></el-cascader>
    </el-form-item>
    <el-form-item label="手机号码" v-if="this.dataForm.id!=null" prop="phone" style="width: 45%">
      <el-input v-model="dataForm.phone" placeholder="手机号码" :disabled="0 != 1" align="right" style="width: 50%"></el-input>
      <el-button type="success" style="width: 30%" @click="dialogVisible = true">变更</el-button>
    </el-form-item>
    <el-form-item label="手机号码" v-if="this.dataForm.id==null" prop="phone" style="width: 45%">
      <el-input v-model="dataForm.phone" placeholder="手机号码" align="right"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email" style="width: 45%">
      <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="工作时间" prop="workTime" style="width: 45%">
      <el-date-picker
        v-model="dataForm.workTime"
        type="month"
        value-format="yyyy-MM-dd"
        placeholder="选择时间"
        style="width: 86%">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="发布机构">
    <el-select v-model="dataForm.clazzId">
      <el-option
        v-for="item in this.clazz"
        :key="item.id"
        :label="item.clazzName"
        :value="item.id">
      </el-option>
    </el-select>
  </el-form-item>
    </el-form>
    <div style="width: 100%" align="right">
      <div style="width: 95%" align="left">
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="dataFormSubmit">确定</el-button>
          <el-button @click="visible = false">取消</el-button>
        </span>
      </div>
    </div>
    <el-dialog
      title="更换手机号码"
      :visible.sync="dialogVisible"
      append-to-body
      width="35%">
      <tr>
        <td>
          <span>当前手机号为，下方验证码通过后，可更换绑定手机号码。</span>
        </td>
      </tr><br/>
      <div style="width: 100%">
        <el-form ref="form" :model="form">
          <el-form-item label="手机号码">
            <el-select v-model="value" placeholder="请选择" style="width: 15%">
              <el-option
                v-for="item in cities"
                :key="item.value"
                :label="item.value"
                :value="item.value">
                <span style="float: left">{{ item.label }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
              </el-option>
            </el-select>
            <el-input v-model="form.phone" placeholder="请输入常用手机号" style="width: 35%"></el-input>
          </el-form-item>
          <el-form-item label="短信验证">
            <el-input v-model="form.code" placeholder="请输入验证码" style="width: 51%"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div style="width: 100%" align="right">
        <div style="width: 93%" align="left">
          <el-button type="primary" @click="updatePhone">保 存</el-button>
          <el-button type="primary" @click="sendMessage">{{sendStatus}}</el-button>
        </div>
      </div>
    </el-dialog>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        addressOptions: [],
        visible: false,
        dialogVisible: false,
        sendStatus: '发送验证码',
        totalTime: 60,
        canClick: true,
        popo:10,
        form: {
          id: '',
          phone: '',
          code: ''
        },
        clazz:[{
          id: '',
          clazzName: '',
          teacherId: ''
        }],
        dataForm: {
          id: null,
          name: '',
          sex: '',
          birthday: '',
          address: [],
          phone: '',
          email: '',
          resumeType: '',
          workTime: '',
          uid: '',
          clazzId:'',
          status:null
        },
        dataRule: {
          name: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          birthday: [
            { required: true, message: '生日不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '手机号码不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          resumeType: [
            { required: true, message: '身份类型不能为空', trigger: 'blur' }
          ],
          workTime: [
            { required: true, message: '工作时间不能为空', trigger: 'blur' }
          ]
        },
        cities: [{
          value: '0086',
          label: '中国'
        }, {
          value: 'Shanghai',
          label: '上海'
        }, {
          value: 'Nanjing',
          label: '南京'
        }, {
          value: 'Chengdu',
          label: '成都'
        }, {
          value: 'Shenzhen',
          label: '深圳'
        }, {
          value: 'Guangzhou',
          label: '广州'
        }],
        value: ''
      }
    },
    methods: {
      init (id) {
        // this.user = JSON.parse(localStorage.getItem('user'));
        // this.dataForm.id=this.user.userId;
        this.dataForm.uid = id
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/resume/resumebasic/findResumeByUid`),
              method: 'get'
            }).then(({data}) => {
              if(data!==''){
                this.dataForm = data
                this.dataForm.address = JSON.parse(data.address)
              }
            })
          }
        })
      },
      editData(val){//处理流数据
        for (let i=0;i<val.length;i++){
          if (val[i].children.length === 0){
            val[i].children = undefined
          }else{
            this.editData(val[i].children)
          }
        }
        return val
      },
      // 表单提交
      dataFormSubmit () {

        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/resume/resumebasic/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'sex': this.dataForm.sex,
                'birthday': this.dataForm.birthday,
                'address': JSON.stringify(this.dataForm.address),
                'phone': this.dataForm.phone,
                'email': this.dataForm.email,
                'resumeType': this.dataForm.resumeType,
                'workTime': this.dataForm.workTime,
                'uid': this.dataForm.uid,
                'clazzId': this.dataForm.clazzId,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                   this.popo=Number(localStorage.getItem("popo")+this.popo);
                   localStorage.setItem("popo",this.popo);
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 发送验证码
      sendMessage () {
        // 如果还未发送可以执刑代码
        if (!this.canClick) return  // 改动的是这两行代码
        // 调用后台mq发送验证码并存入redis，一分钟有效期
        this.$http({
          url: this.$http.adornUrl(`/resume/resumebasic/send/${this.form.phone}`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            console.log(data)
          }
        })
        // 将状态改为已发送
        this.canClick = false
        // 倒计时开始，时间为1分钟
        this.sendStatus = this.totalTime + 's后重新发送'
        let clock = window.setInterval(() => {
          this.totalTime--
          this.sendStatus = this.totalTime + 's后重新发送'
          if (this.totalTime < 0) {
            window.clearInterval(clock)
            // 倒计时结束后可重新发送
            this.sendStatus = '重新发送验证码'
            this.totalTime = 10
            this.canClick = true  // 这里重新开启
          }
        }, 1000)
      },
      // 修改手机号
      updatePhone () {
        this.$http({
          url: this.$http.adornUrl(`/resume/resumebasic/updatePhone/${this.form.phone}/${this.form.code}`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataForm.phone = this.form.phone
            this.$message.success('变更完成')
            this.dialogVisible = false
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      findAddressData(){
        this.$http({
          url: this.$http.adornUrl('/hope/addresss'),
          method: 'get'
        }).then(({data}) => {
          this.addressOptions = this.editData(data)
        })
      }
    },
    created(){
       this.findAddressData();
       //去后台查询机构下拉框数据
      this.$http({
        url: this.$http.adornUrl('/sys/clazz/findClassAll'),
        method: 'get'
      }).then(({data}) => {
        this.clazz = this.editData(data).list
      })
    }
  }
</script>
